{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to copy a QComponent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start by importing QisKit Metal:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import qiskit_metal as metal\n",
    "from qiskit_metal import designs, draw\n",
    "from qiskit_metal import MetalGUI, Dict #, open_docs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then let's fire up the GUI:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "design = designs.DesignPlanar()\n",
    "gui = MetalGUI(design)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we'll put one transmon at the origin:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's start by putting a transmon at the origin:\n",
    "from qiskit_metal.qlibrary.qubits.transmon_pocket import TransmonPocket\n",
    "design.overwrite_enabled = True\n",
    "q1 = TransmonPocket(design, 'Q1')\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, we'll copy the component and then manually modify the coordinates to be at (1,0) instead of (0,0):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now let's copy the transmon at the origin and put it at position (0,1):\n",
    "q1_copy = design.copy_qcomponent(q1, 'Q1_copy')\n",
    "q1_copy.options['pos_x']='1.0mm'\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we'll copy the original component and in the same step we'll move the copy to (-1,0) by passing a dictionary to the \"copy_qcomponent\" command:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now let's copy the transmon at the origin and put it at position (0,-1):\n",
    "q1_anothercopy = design.copy_qcomponent(q1,'Q1_another_copy', dict(pos_x='-1.0mm'))\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can copy multiple components at once using the \"copy_multiple_qcomponents\" command. Here's an example that copies the three components we've just created (Q1 and the two copies: Q1_copy and Q1_another_copy) and moves them up by 2.0mm each: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now let's copy all three components at the same time, moving them up by +2mm in the y-direction:\n",
    "newcopies = design.copy_multiple_qcomponents([q1, q1_copy, q1_anothercopy], ['Q3', 'Q4', 'Q5'], [dict(pos_y='1.0mm'), dict(pos_y='2.0mm'), dict(pos_y='3.0mm')])\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that we can also copy multiple qcomponents without passing dictionaries. In this case, the copied qcomponents will sit on top of of the original qcomponents in the layout:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example of copying without giving any dictionary values; qcomponents will sit on top of the originals!\n",
    "newcopies2 = design.copy_multiple_qcomponents([q1, q1_copy, q1_anothercopy], ['Q6', 'Q7', 'Q8'])\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note also that we can copy multiple qcomponents but only give a dictionary to one of them. The other two dictionaries still need to exist but can be empty: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copy the three original components but only give a dictionary for the first one; other two dictionaries still need to exist but can be empty:\n",
    "newcopies3 = design.copy_multiple_qcomponents([q1, q1_copy, q1_anothercopy], ['Q9', 'Q10', 'Q11'], [dict(pos_y='-1.0mm'), dict(), dict()])\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
